package com.example.library_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.library_demo.dto.user.UserLoginDTO;
import com.example.library_demo.entity.User;
import com.example.library_demo.mapper.UserMapper;
import com.example.library_demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.library_demo.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Eric
 * @since 2024-11-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User getUserByUsernameAndPassword(UserLoginDTO userLoginDTO) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername,userLoginDTO.getUsername())
                .eq(User::getPassword,userLoginDTO.getPassword());
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public User getUserByToken(String token) {
        LocalDateTime expTime = LocalDateTime.now().plusHours(-2);
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getToken,token)
                .ge(User::getLoginDatetime,expTime);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public UserInfoVO getUserInfoByUserId(Long userId) {
        return baseMapper.selectUserAndRoleByUserId(userId);
    }

}
